<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;

use Imi\Db\Db;
use Imi\Util\Random;
use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use ImiDemo\HttpDemo\MainServer\Model\Test;
use ImiDemo\HttpDemo\MainServer\Model\User;
use ImiDemo\HttpDemo\MainServer\Model\UserSns;
use ImiDemo\HttpDemo\MainServer\Model\UserFriend;
use ImiDemo\HttpDemo\MainServer\Model\UserWithFriend;
use Imi\Util\ObjectArrayHelper;
use ImiDemo\HttpDemo\MainServer\Model\UserRole;

/**
 * Model操作 演示
 * @Controller("/ModelManyToMany/")
 * @View(renderType="json")
 */
class ModelManyToManyController extends HttpController
{
    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelManyToMany/find
     * 
     * @Action
     * @return void
     */
    public function find()
    {
        $user = UserWithFriend::find(1);
        return $user;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelManyToMany/select
     * 
     * @Action
     * @return void
     */
    public function select()
    {
        $list = UserWithFriend::select();

        return $list;
    }
    
    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelManyToMany/newInstance
     * 
     * @Action
     * @return void
     */
    public function newInstance()
    {
        $user = UserWithFriend::newInstance();

        return $user;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelManyToMany/insert
     * 
     * @Action
     * @return void
     */
    public function insert()
    {
        $user = UserWithFriend::newInstance();
        $user->username = Random::letterAndNumber(6, 16);
        $user->ex->intro = '这个人很懒，什么也没留下';
        $user->userRole->append(
            UserRole::newInstance(['role_id'=>1]), 
            UserRole::newInstance(['role_id'=>2])
        );
        $result = $user->insert();

        return [
            'success'    =>    $result->getLastInsertId() > 0,
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelManyToMany/update
     * 
     * @Action
     * @return void
     */
    public function update()
    {
        $user = UserWithFriend::find(1);
        $user->ex->intro = '这个人很懒，什么也没留下-' . date('Y-m-d H:i:s');
        $user->userRole->clear();
        $user->userRole->append(
            UserRole::newInstance(['role_id'=>998])
        );
        $result = $user->update();

        return [
            'success'    =>    $result->isSuccess(),
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelManyToMany/delete
     * 
     * @Action
     * @return void
     */
    public function delete()
    {
        $maxId = UserWithFriend::query()->max('id');
        $user = UserWithFriend::find($maxId);
        $result = $user->delete();

        return [
            'success'    =>    $result->isSuccess(),
        ];
    }
}